package study.datastructure.list;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>单链表,使用单链表模拟水浒传英雄排行榜</p>
 *
 * @author LiShaoWen
 * @date 2021/10/21 22:14
 */
public class SingleLinkedListDemo {
    public static void main(String[] args) {
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode heroNode2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode heroNode3 = new HeroNode(3, "无用", "智多星");
        singleLinkedList.add(heroNode1);
        singleLinkedList.add(heroNode2);
        singleLinkedList.add(heroNode3);
        singleLinkedList.print();
    }
}

class SingleLinkedList {
    // 初始化一个头结点，不存放具体数据
    private HeroNode head = new HeroNode(0, "", "");

    public void add(HeroNode heroNode) {
        HeroNode tempNode = head;
        // 遍历找到链表的最后一个节点
        while (tempNode.next != null) {
            tempNode = tempNode.next;
        }
        tempNode.next = heroNode;
    }

    public void addByOrder(HeroNode heroNode) {
        // 如果链表为空直接插入
        if (head.next == null) {
            head.next = heroNode;
            return;
        }
        HeroNode tempNode = head.next;
        // 遍历
        while (tempNode.next != null){
            //if (tempNode.)
        }
    }

    // 打印链表
    public void print() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode tempNode = head.next;
        // 遍历找到链表的最后一个节点
        while (tempNode != null) {
            System.out.println(tempNode);
            tempNode = tempNode.next;
        }
    }
}

class HeroNode {
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;

    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
